package com.lgj.demo0812.TestDemo;

import org.springframework.util.StopWatch;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lizy51
 * @date 2022-10-12 10:32
 */
public class IOTest {
    public static void main(String[] args) throws Exception {
//        //批量删除文件
//        String filePath = "/Users/liguangjiang/devTools/dataFile/test";

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        String filePath = "/Users/liguangjiang/devTools/dataFile/dataFile/123.json";
        String prex = "F:\\Demo\\dataFile\\";
//        System.out.println(deleteFile(filePath,4));

//        String filePath = "/Users/liguangjiang/devTools/dataFile/address_md_data_33.json";
//        String prex = "/Users/liguangjiang/devTools/dataFile/";
//        String filePath = "/Users/liguangjiang/devTools/dataFile/example.json";
    writer(filePath,readFile(filePath));
//        splitFile(prex, reload(filePath));
//        //heel(filePath);
        stopWatch.stop();
        System.out.printf("执行时长：%g 秒.%n", stopWatch.getTotalTimeSeconds()); // %n 为换行®
    }


    /**
     * 读取文件
     * des:驼峰命名 ab_cd修改成abCd
     *
     * @param filePath
     */
    public static List<String> heel(String filePath) {
        BufferedReader br = null;

        String line = null;
        List<String> list = new ArrayList<>();
        StringBuffer buf = new StringBuffer();

        try {
            //根据文件路径创建缓冲输入流
            br = new BufferedReader(new FileReader(filePath));
            //循环读取文件的每一行,对需要进行修改的行进行修改,放入缓存对象中
            while ((line = br.readLine()) != null) {
                //此处根据实际需要修改某些行的内容
                if (line.contains("_")) {
                    String[] words = line.split("_");
                    for (int i = 0; i < words.length; i++) {
                        if (i == 0) {
                            buf.append(words[0]);
                        } else {
                            buf.append(captureName(words[i]));
                        }
                    }
                }
                //使用list记录所有的数据
                list.add(buf.toString());
//                System.out.println(buf.toString());
                //清空可变字符串,重新记录数据
                buf.delete(0, buf.length());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 将文件回写到文件中
     */
    public static void writer(String filePath, List<String> content) {
        BufferedWriter bw = null;
        try {
            //根据文件路径创建缓冲输出流
            bw = new BufferedWriter(new FileWriter(filePath));
            //将内容写入到文件中
            for (int i = 0; i < content.size(); i++) {
                bw.write(content.get(i));
                bw.newLine();
                bw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //拆文件
    public static void splitFile(String prex, List<String> content) {
        BufferedWriter bw = null;
        int mark = 0;
        try {
            //根据文件路径创建缓冲输出流
            //将内容写入到文件中
            for (int i = 0; i < content.size(); i++) {
                if (i % 6000 == 0 ) {
                    if ( i > 0) System.out.println(i+":"+content.get(i-1));
                    mark++;
                    String fileName = prex + "test" + mark + ".json";
                    bw = new BufferedWriter(new FileWriter(fileName));
//                    bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (fileName,true),"UTF-8"));
                    System.out.println(mark);
                }
                bw.write(content.get(i));
                bw.newLine();
                bw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //首字母大写
    public static String captureName(String name) {
        //     name = name.substring(0, 1).toUpperCase() + name.substring(1);
//        return  name;
        char[] cs = name.toCharArray();
        cs[0] -= 32;
        return String.valueOf(cs);
    }

    //加前缀
    public static List<String> appendIndex(String filePath) {
        BufferedReader br = null;
        String line = null;
        List<String> list = new ArrayList<>();
        StringBuffer buf = new StringBuffer();
        try {
            //根据文件路径创建缓冲输入流
            br = new BufferedReader(new FileReader(filePath));
            //循环读取文件的每一行,对需要进行修改的行进行修改,放入缓存对象中
            while ((line = br.readLine()) != null) {
                if (line.contains("},")) {
                    String s = "}\n{\"index\":{\"_index\":\"index-addr-address-base\"}}";
                    String replace = line.replace("},", s);
                    buf.append(replace);
                } else {
                    buf.append(line);
                }
//                buf.append('"').append(" ").append((int) (Math.random() * 2)).append(line.substring(3));
                //使用list记录所有的数据
                list.add(buf.toString());
                System.out.println(buf.toString());
                //清空可变字符串,重新记录数据
                buf.delete(0, buf.length());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    //数据整理
    public static List reload(String filePath) {
        BufferedReader br = null;
        String line = null;
        List<String> list = new ArrayList<>();
        StringBuffer buf = new StringBuffer();
        try {
            //根据文件路径创建缓冲输入流
            br = new BufferedReader(new FileReader(filePath));
            //循环读取文件的每一行,对需要进行修改的行进行修改,放入缓存对象中
            while ((line = br.readLine()) != null) {
                if (line.contains("{")) {
                    //开始
                    line = "{\"index\":{\"_index\":\"index-addr-address-base\",\"_id\":flg99}}\n{";
                    buf.append(line);
                } else if (line.contains("updateOrgName")) {
                    //结尾
                    String end = line.trim();
                    buf.append(end + "}");
                } else if (line.contains("\"addrId\"")) {
                    String trim = line.trim();
                    String addrId = trim.substring(10, trim.length() - 1);
                    String newBuf = buf.toString().replace("flg99", addrId);
                    buf.delete(0, buf.length());
                    buf.append(newBuf);
                    buf.append(trim);
                } else if (line.contains("}")) {
                    //清空可变字符串,重新记录数据
                    list.add(buf.toString());
                    buf.delete(0, buf.length());
                } else {
                    buf.append(line.trim());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public static void newSpilt(String filePath) throws IOException {
        BufferedWriter bw = null;
        BufferedReader br = null;
        String line = null;
//        List<String> list = new ArrayList<>();
        StringBuffer buf = new StringBuffer();
        bw = new BufferedWriter(new FileWriter(filePath));
        try {
            //根据文件路径创建缓冲输入流
            br = new BufferedReader(new FileReader(filePath));
            //循环读取文件的每一行,对需要进行修改的行进行修改,放入缓存对象中
            while (br.readLine() != null) {
                line = br.readLine();
                if (line.contains("{")) {
                    //开始
                    line = "{\"index\":{\"_index\":\"index-addr-address-base\",\"_id\":flg99}}\n{";
                    buf.append(line);
                } else if (line.contains("updateOrgName")) {
                    //结尾
                    String end = line.trim();
                    buf.append(end + "}\n");
                } else if (line.contains("\"addrId\"")) {
                    String trim = line.trim();
                    String addrId = trim.substring(10, trim.length() - 1);
                    String newBuf = buf.toString().replace("flg99", addrId);
                    buf.delete(0, buf.length());
                    buf.append(newBuf);
                    buf.append(trim);
                } else if (line.contains("}")) {
                    //清空可变字符串,重新记录数据
//                    list.add(buf.toString());
                    bw.write(buf.toString());
                    buf.delete(0, buf.length());
                } else {
                    buf.append(line.trim());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                bw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static Boolean deleteFile(String fileName,Integer num) {
        for (int i = 1; i < num; i++) {
            fileName = fileName+i+".json";
            File file = new File(fileName);
            return file.delete();
        }
        return null;
    }

    public static List readFile(String filePath) {
        BufferedReader br = null;
        String line = null;
        List<String> list = new ArrayList<>();
        StringBuffer buf = new StringBuffer();
        try {
            //根据文件路径创建缓冲输入流
            br = new BufferedReader(new FileReader(filePath));
            //循环读取文件的每一行,对需要进行修改的行进行修改,放入缓存对象中
            while ((line = br.readLine()) != null) {
                buf.append(line);
                if (line.contains("updateOrgName")){
                    buf.append("}");
                }
                list.add(buf.toString());
//                System.out.println(buf.toString());
                //清空可变字符串,重新记录数据
                buf.delete(0, buf.length());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }

}
